/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fy.input.service;

import fy.input.sql.MySQLSetManager;
import java.io.OutputStream;
import java.util.Map;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

/**
 *生成导出Excel文件
 * @author Administrator
 */
public class ZcExcelView extends AbstractExcelView {
    
    private String[] args;
    private String sqlName;
    
    public ZcExcelView(String[] args, String sqlName) {
        this.args = args;
        this.sqlName = sqlName;
    }
    
    @Override
    protected void buildExcelDocument(Map<String, Object> obj,
            HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        HSSFSheet sheet = workbook.createSheet("导出");
        List<List<String>> rows = MySQLSetManager.getInstance().query_expExcel(args, sqlName);
        HSSFCell cell = getCell(sheet, 0, 0);
        setText(cell, "Spring Excel test");
        cell = getCell(sheet, 1, 0);
        for (short i = 0; i < 10; i++) {
        }
        for (int i = 0, rowSize = rows.size(); i < rowSize; i++) {
            List<String> row = rows.get(i);
            for (int j = 0, columnCount = row.size(); j < columnCount; j++) {
                setText(getCell(sheet, i, j), row.get(j));
            }
        }
        
        String filename = "exp.xls";//设置下载时客户端Excel的名称     
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename=" + filename);
        OutputStream ouputStream = response.getOutputStream();
        workbook.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();
    }
}
